﻿using Kdbndp.TypeHandlers;
using MediatR;
using Purchase.ApiS.ListDisplay.Command;
using Purchase.Domian.Model;
using Purchase.ErrorCode;
using Purchase.Infrastructrue.Implenment;

namespace Purchase.ApiS.ListDisplay.Hanlder
{
    public class GetMaterialInfoCommandHanlder : IRequestHandler<GetMaterialInfoCommand, ApiResult<MaterialInfo>>
    {
        public readonly ILogger<GetMaterialInfoCommandHanlder> _logger;
        public readonly IBaseRepostity<MaterialInfo> _materialInfoRepostity;

        public GetMaterialInfoCommandHanlder(ILogger<GetMaterialInfoCommandHanlder> logger, IBaseRepostity<MaterialInfo> materialInfoRepostity)
        {
            _logger = logger;
            _materialInfoRepostity = materialInfoRepostity;
        }
        /// <summary>
        /// 获取物料信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public Task<ApiResult<MaterialInfo>> Handle(GetMaterialInfoCommand request, CancellationToken cancellationToken)
        {
            ApiResult<MaterialInfo> result = new ApiResult<MaterialInfo>();
            try
            {
                var mate = _materialInfoRepostity.GetAll().ToList();
                result.Datas = mate;
                result.Msg = "获取物料信息成功";
                _logger.LogInformation("获取物料信息成功");
                result.Code = ApiEume.Success;
            }
            catch (Exception ex)
            {
                 result.Msg = "获取物料信息失败";
            }
            return Task.FromResult(result);
        }
    }
}
